﻿using System.Collections.Generic;

namespace Battle.TimelineLog
{
    /// <summary>
    /// 浮点型修饰器
    /// </summary>
    public class FloatModifier
    {
        public int FromId;// 来源的动态id，一般来自buff的id, 一般来说，一个buff对应一个修改
        public float Value;
    }

    /// <summary>
    /// 浮点型修饰器集合
    /// </summary>
    public class FloatModifierCollection
    {
        public float TotalValue { get; private set; }
        private List<FloatModifier> Modifiers { get; } = new List<FloatModifier>();

        public float Add(FloatModifier modifier)
        {
            Modifiers.Add(modifier);
            Update();
            return TotalValue;
        }
        public float Add(int fromId, float value)
        {
            FloatModifier fm = new FloatModifier();
            fm.FromId = fromId;
            fm.Value = value;
            Modifiers.Add(fm);
            Update();
            return TotalValue;
        }

        public float Remove(FloatModifier modifier)
        {
            Modifiers.Remove(modifier);
            Update();
            return TotalValue;
        }

        public float Remove(int fromId)
        {
            for (int i = Modifiers.Count - 1; i >= 0; i--)
            {
                if (Modifiers[i].FromId == fromId)
                {
                    Modifiers.RemoveAt(i);
                    break;
                }
            }
            Update();
            return TotalValue;
        }

        public void Update()
        {
            TotalValue = 0;
            foreach (var item in Modifiers)
            {
                TotalValue += item.Value;
            }
        }
    }
    /// <summary>
    /// 浮点型数值
    /// </summary>
    public class FloatProp
    {
        /// <summary>
        /// 最终值
        /// </summary>
        /// <value></value>
        public float Value { get; private set; }
        public float baseValue { get; private set; }
        public float add { get; private set; }
        public float pctAdd { get; private set; }
        private FloatModifierCollection AddCollection { get; } = new FloatModifierCollection();
        private FloatModifierCollection PctAddCollection { get; } = new FloatModifierCollection();

        public FloatProp() { }
        public FloatProp(float baseValue) { SetBase(baseValue); }

        public void Initialize()
        {
            baseValue = add = pctAdd = 0f;
        }
        public float SetBase(float value)
        {
            baseValue = value;
            Update();
            return baseValue;
        }
        public void AddAddModifier(FloatModifier modifier)
        {
            add = AddCollection.Add(modifier);
            Update();
        }
        public void AddPctAddModifier(FloatModifier modifier)
        {
            pctAdd = PctAddCollection.Add(modifier);
            Update();
        }

        public void RemoveAddModifier(FloatModifier modifier)
        {
            add = AddCollection.Remove(modifier);
            Update();
        }
        public void RemovePctAddModifier(FloatModifier modifier)
        {
            pctAdd = PctAddCollection.Remove(modifier);
            Update();
        }

        public void Update()
        {
            // 这里的算法由数值策划给到，这里只是一个示例
            var value1 = baseValue;
            var value2 = (value1 + add) * (100 + pctAdd) / 100f;
            Value = value2;
        }
    }
}